package com.example.user.controller;
import com.example.common.Dto;
import com.example.common.DtoUtil;
import com.example.pojo.GdUser;
import com.example.user.service.LoginService;
import com.example.utils.Constants;
import com.example.utils.EmptyUtils;
import com.example.vo.TokenVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;

@Controller
@RequestMapping(value = "/api")
public class LoginController {

    @Resource
    private LoginService loginService;

    /**
     * 用户登录
     * @param userCode
     * @param password
     * @param response
     * @param request
     * @return
     */
    @ApiOperation(value="用户登录",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="用户登录")
    @RequestMapping(value = "/dologin", method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto dologin(String userCode, String password, HttpServletResponse response, HttpServletRequest request) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        if (!EmptyUtils.isEmpty(userCode) && !EmptyUtils.isEmpty(password)) {
            GdUser user = null;
            try {
                user = loginService.login(userCode.trim(), password);
             } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail(e.getMessage(), Constants.User_AUTH.AUTH_UNKNOWN);
            }
            if (EmptyUtils.isNotEmpty(user)) {
                String newToken = loginService.generateToken(user);
                loginService.save(newToken, user);
                TokenVO tokenVO = new TokenVO(newToken, Calendar.getInstance().getTimeInMillis() + Constants.Redis_Expire.SESSION_TIMEOUT * 1000, Calendar.getInstance().getTimeInMillis());
                return DtoUtil.returnSuccess("登录成功",tokenVO);
            } else{
                return DtoUtil.returnFail("用户名密码错误", Constants.User_AUTH.AUTH_AUTHENTICATION_FAILED);
            }
        } else {
            return DtoUtil.returnFail("参数错误！检查提交的参数名称是否正确。", Constants.User_AUTH.AUTH_PARAMETER_ERROR);
        }
    }

    /**
     * 用户注销
     * @param request
     * @return
     */
    @ApiOperation(value="用户注销",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="用户注销")
    @RequestMapping(value = "/logout", method = RequestMethod.GET, produces = "application/json", headers = "token")
    @ResponseBody
    public Dto logout(HttpServletRequest request) {
        //验证token
        String token = request.getHeader("token");
        if (!loginService.validate(token)) {
            return DtoUtil.returnFail("token无效", Constants.User_AUTH.AUTH_TOKEN_INVALID);
        }
        try {
            //删除token和信息
            loginService.delete(token);
            return DtoUtil.returnSuccess("注销成功");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("注销失败", Constants.User_AUTH.AUTH_UNKNOWN);
        }
    }
}
